//该文件负责crm的页面元素操作
define(function(require, exports, module) {
    'use strict';

    var rk = require('./rk');

    rk.crm = {};

    (function() {
        var crmPanelSel = '#crm-panel';
        var crmContainerSel = '#div_main';
        var detailSel = '#crm_detail';
        var newDetailSel = '#crmContentDetail'; {
            $(document.body).on('click.crmGlobalEvents', function(e) {
                if ($(detailSel).is(':visible')) {
                    var target = $(e.target);
                    if (target.parents(crmContainerSel).size() > 0 || target.is(crmContainerSel)) {
                        if (target.parents(detailSel).size() > 0 || target.is(detailSel)) return;
                        if (target.parents('a[customerid]').size() > 0 || target.is('a[customerid]')) return;
                        rk.crm.hideDetail();
                    }
                }
            });
        }

        rk.crm.detailPopupWay = 'new'; //old, new

        var groupId, detailId, BelongId;

        $.extend(rk.crm, {
            setBelongId: function(id) {
                BelongId = id;
            },
            getBelongId: function() {
                return BelongId;
            },
            setDetailId: function(id) {
                detailId = id;
            },
            getDetailId: function() {
                return detailId;
            },
            setGroupId: function(id) {
                groupId = id;
            },
            getGroupId: function() {
                return groupId;
            },
            initPanel: function(appName, opt) {
                var crmPanel = rk.showPageView('crm').off();
                var pathname = window.location.pathname;
                var shouldActAsDetail = rk.crm.shouldActAsDetail();
                if (shouldActAsDetail) {
                    pathname = pathname.replace('/final', '');
                }
                if (typeof appName == 'undefined' || shouldActAsDetail) {
                    appName = pathname.match(/[a-zA-Z_-]{1,}/)[0];
                }
                if (shouldActAsDetail) appName = appName + '_detail';
                if (typeof opt == 'undefined') opt = {};
                opt._appName = appName;
                if (opt._appName.indexOf('_detail') > 0) {
                    rk.formMgr.setScrollingContainer(crmPanel); //滚动懒加载的依据
                    crmPanel.css('overflow-y', 'auto');
                    crmPanel.parent().addClass('crm-detail-panel-inner');
                }
                crmPanel[appName](opt);
            },
            popupDetail: function(appName, opt) {
                if (opt.belongid) {
                    window.open('/' + appName + '_detail.action?id=' + opt.instanceid + '&belongId=' + opt.belongid);
                } else {
                    window.open('/' + appName + '_detail.action?id=' + opt.customerid);
                }
            },
            popupDetail2: function(appName, opt) {
                var me = this;
                window.clearTimeout(me.hideDetailTimer);
                if (typeof appName == 'undefined') appName = 'undefinedApp';
                if (typeof opt == 'undefined') opt = {};

                var widgetName = 'crm_' + appName + '_detail_popup'; //比如apps_account_detail
                var ctxUrl = 'crm/js/apps/' + appName;
                require.async([
                    ctxUrl + '/detail/widget', //所需的js文件
                ], function(loader, tmpl) {

                    //销毁旧的
                    rk.crm.destroyDetail();

                    //创建新的
                    $(crmPanelSel).append('<div id="crmContentDetail" class="crm-content-detail">' + tmpl + '</div>');
                    var crmDetailElem = $(newDetailSel);

                    $.fn[widgetName] ? crmDetailElem[widgetName](opt) : false;
                    crmDetailElem.data('widgetName', widgetName);
                    crmDetailElem.css('z-index', 999999999999);
                    //debug
                    $('[is-rk-widget="true"]').css('border', '2px solid red')

                });
            },
            showDetail: function() {
                var me = this;
                window.clearTimeout(me.hideDetailTimer);
                var crmDetailElem = $(newDetailSel);
                crmDetailElem.show();
            },
            hideDetailTimer: null,
            hideDetail: function() {
                var me = this;
                me.hideDetailTimer = window.setTimeout(function() {
                    var crmDetailElem = $(newDetailSel);
                    crmDetailElem.hide();
                }, 100);
            },
            closeDetail: function() {
                rk.crm.destroyDetail();
            },
            getDetail: function() {},
            destroyDetail: function() {
                var crmDetailElem = $(newDetailSel);
                try {
                    var prevWidgetName = crmDetailElem.data('widgetName');
                    if (prevWidgetName) crmDetailElem[prevWidgetName]('destroy');
                } catch (e) {
                    //不必抛错
                }
                $(newDetailSel).off().remove();
            },
            extendBiz: function(biz) {
                $.extend(rk.crm, biz);
            },
            shouldActAsDetail: function() {
                var pathname = window.location.pathname;
                return (pathname.indexOf('/final/account.') >= 0
                    || pathname.indexOf('/final/customize.') >= 0
                    || pathname.indexOf('/final/opportunity.') >= 0);
            },
            isDetailPage: function() {
                var me = this;
                return /[\w]+_detail./ig.test(window.location.href) || me.shouldActAsDetail();
            }
        });
    })();

    module.exports = rk;
});
